<clickhouse>
  <logger>
    <level>trace</level>
    <log>{{ ck_datadir }}/log/clickhouse-server/clickhouse-server.log</log>
    <errorlog>{{ ck_datadir }}/log/clickhouse-server/clickhouse-server.err.log</errorlog>
    <size>1000M</size>
    <count>10</count>
  </logger>

  <http_options_response>
    <header>
      <name>Access-Control-Allow-Origin</name>
      <value>*</value>
    </header>
    <header>
      <name>Access-Control-Allow-Headers</name>
      <value>origin, x-requested-with, x-clickhouse-format, x-clickhouse-user, x-clickhouse-key, Authorization</value>
    </header>
    <header>
      <name>Access-Control-Allow-Methods</name>
      <value>POST, GET, OPTIONS</value>
    </header>
    <header>
      <name>Access-Control-Max-Age</name>
      <value>86400</value>
    </header>
  </http_options_response>

  <http_port>{{ ck_http_port }}</http_port>
  <tcp_port>{{ ck_tcp_port }}</tcp_port>
  <mysql_port>9004</mysql_port>
  <postgresql_port>9005</postgresql_port>
  <interserver_http_port>9009</interserver_http_port>
  <interserver_http_host>{{ ansible_host }}</interserver_http_host>
  <listen_host>0.0.0.0</listen_host>
  <max_connections>4096</max_connections>
  <keep_alive_timeout>10</keep_alive_timeout>

  <openSSL>
    <server> 
      <!-- <dhParamsFile>/etc/clickhouse-server/dhparam.pem</dhParamsFile>-->
      <verificationMode>none</verificationMode>
      <loadDefaultCAFile>true</loadDefaultCAFile>
      <cacheSessions>true</cacheSessions>
      <disableProtocols>sslv2,sslv3</disableProtocols>
      <preferServerCiphers>true</preferServerCiphers>

      <invalidCertificateHandler>
        <name>RejectCertificateHandler</name>
      </invalidCertificateHandler>
    </server>

    <client> <!-- Used for connecting to https dictionary source and secured Zookeeper communication -->
      <loadDefaultCAFile>true</loadDefaultCAFile>
      <cacheSessions>true</cacheSessions>
      <disableProtocols>sslv2,sslv3</disableProtocols>
      <preferServerCiphers>true</preferServerCiphers>
      <!-- Use for self-signed: <verificationMode>none</verificationMode> -->
      <invalidCertificateHandler>
        <!-- Use for self-signed: <name>AcceptCertificateHandler</name> -->
        <name>RejectCertificateHandler</name>
      </invalidCertificateHandler>
    </client>
  </openSSL>

  <concurrent_threads_soft_limit_num>0</concurrent_threads_soft_limit_num>
  <concurrent_threads_soft_limit_ratio_to_cores>2</concurrent_threads_soft_limit_ratio_to_cores>

  <!-- Maximum number of concurrent queries. -->
  <max_concurrent_queries>1000</max_concurrent_queries>

  <max_server_memory_usage>0</max_server_memory_usage>

  <max_thread_pool_size>10000</max_thread_pool_size>

  <!-- Configure other thread pools: -->
  <!--
  <background_buffer_flush_schedule_pool_size>16</background_buffer_flush_schedule_pool_size>
  <background_pool_size>16</background_pool_size>
  <background_merges_mutations_concurrency_ratio>2</background_merges_mutations_concurrency_ratio>
  <background_merges_mutations_scheduling_policy>round_robin</background_merges_mutations_scheduling_policy>
  <background_move_pool_size>8</background_move_pool_size>
  <background_fetches_pool_size>8</background_fetches_pool_size>
  <background_common_pool_size>8</background_common_pool_size>
  <background_schedule_pool_size>128</background_schedule_pool_size>
  <background_message_broker_schedule_pool_size>16</background_message_broker_schedule_pool_size>
  <background_distributed_schedule_pool_size>16</background_distributed_schedule_pool_size>
  <tables_loader_foreground_pool_size>0</tables_loader_foreground_pool_size>
  <tables_loader_background_pool_size>0</tables_loader_background_pool_size>
  -->

    <max_server_memory_usage_to_ram_ratio>0.9</max_server_memory_usage_to_ram_ratio>
    <total_memory_profiler_step>4194304</total_memory_profiler_step>
    <total_memory_tracker_sample_probability>0</total_memory_tracker_sample_probability>
    <uncompressed_cache_size>8589934592</uncompressed_cache_size>
    <mark_cache_size>5368709120</mark_cache_size>
    <index_mark_cache_size>5368709120</index_mark_cache_size>
    <mmap_cache_size>1000</mmap_cache_size>

    <compiled_expression_cache_size>134217728</compiled_expression_cache_size>

    <compiled_expression_cache_elements_size>10000</compiled_expression_cache_elements_size>

    <custom_cached_disks_base_directory>{{ ck_datadir }}/caches/</custom_cached_disks_base_directory>

    <validate_tcp_client_information>false</validate_tcp_client_information>

    <!-- Path to data directory, with trailing slash. -->
    <path>{{ ck_datadir }}/</path>
    <!-- Path to temporary data for processing hard queries. -->
    <tmp_path>{{ ck_datadir }}/tmp/</tmp_path>
    <!-- Disable AuthType plaintext_password and no_password for ACL. -->
    <allow_plaintext_password>1</allow_plaintext_password>
    <allow_no_password>1</allow_no_password>
    <allow_implicit_no_password>1</allow_implicit_no_password>
    <!-- When a user does not specify a password type in the CREATE USER query, the default password type is used.
         Accepted values are: 'plaintext_password', 'sha256_password', 'double_sha1_password', 'bcrypt_password'.
      -->
    <default_password_type>sha256_password</default_password_type>
    <!-- Work factor for bcrypt_password authentication type-->
    <bcrypt_workfactor>12</bcrypt_workfactor>

    <!-- <tmp_policy>tmp</tmp_policy> -->

    <!-- Directory with user provided files that are accessible by 'file' table function. -->
    <user_files_path>{{ ck_datadir }}/user_files/</user_files_path>

    <!-- Sources to read users, roles, access rights, profiles of settings, quotas. -->
    <user_directories>
        <users_xml>
            <!-- Path to configuration file with predefined users. -->
            <path>users.xml</path>
        </users_xml>
        <local_directory>
            <!-- Path to folder where users created by SQL commands are stored. -->
            <path>{{ ck_datadir }}/access/</path>
        </local_directory>
    </user_directories>

    <access_control_improvements>

        <users_without_row_policies_can_read_rows>true</users_without_row_policies_can_read_rows>
        <on_cluster_queries_require_cluster_grant>true</on_cluster_queries_require_cluster_grant>
        <select_from_system_db_requires_grant>true</select_from_system_db_requires_grant>
        <select_from_information_schema_requires_grant>true</select_from_information_schema_requires_grant>
        <settings_constraints_replace_previous>true</settings_constraints_replace_previous>
        <role_cache_expiration_time_seconds>600</role_cache_expiration_time_seconds>
    </access_control_improvements>

    <!-- Default profile of settings. -->
    <default_profile>default</default_profile>
    <custom_settings_prefixes>SQL_</custom_settings_prefixes>
    <default_database>{{ ck_default_database }}</default_database>
    <mlock_executable>true</mlock_executable>
    <!-- Reallocate memory for machine code ("text") using huge pages. Highly experimental. -->
    <remap_executable>false</remap_executable>

    <![CDATA[
         Uncomment below in order to use JDBC table engine and function.

         To install and run JDBC bridge in background:
         * [Debian/Ubuntu]
           export MVN_URL=https://repo1.maven.org/maven2/com/clickhouse/clickhouse-jdbc-bridge/
           export PKG_VER=$(curl -sL $MVN_URL/maven-metadata.xml | grep '<release>' | sed -e 's|.*>\(.*\)<.*|\1|')
           wget https://github.com/ClickHouse/clickhouse-jdbc-bridge/releases/download/v$PKG_VER/clickhouse-jdbc-bridge_$PKG_VER-1_all.deb
           apt install --no-install-recommends -f ./clickhouse-jdbc-bridge_$PKG_VER-1_all.deb
           clickhouse-jdbc-bridge &

         * [CentOS/RHEL]
           export MVN_URL=https://repo1.maven.org/maven2/com/clickhouse/clickhouse-jdbc-bridge/
           export PKG_VER=$(curl -sL $MVN_URL/maven-metadata.xml | grep '<release>' | sed -e 's|.*>\(.*\)<.*|\1|')
           wget https://github.com/ClickHouse/clickhouse-jdbc-bridge/releases/download/v$PKG_VER/clickhouse-jdbc-bridge-$PKG_VER-1.noarch.rpm
           yum localinstall -y clickhouse-jdbc-bridge-$PKG_VER-1.noarch.rpm
           clickhouse-jdbc-bridge &

         Please refer to https://github.com/ClickHouse/clickhouse-jdbc-bridge#usage for more information.
    ]]>

    <!-- ck集群名 -->
    <remote_servers incl="clickhouse_remote_servers"></remote_servers>
    <!-- 集群配置，副本配置 -->
    <include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>
    <!-- 引用zk -->
    <zookeeper incl="zookeeper-servers" optional="true" />
    <!-- 同步表的配置 -->
    <macros incl="macros" optional="true" />


    <!-- Reloading interval for embedded dictionaries, in seconds. Default: 3600. -->
    <builtin_dictionaries_reload_interval>3600</builtin_dictionaries_reload_interval>
    <!-- Maximum session timeout, in seconds. Default: 3600. -->
    <max_session_timeout>3600</max_session_timeout>
    <!-- Default session timeout, in seconds. Default: 60. -->
    <default_session_timeout>60</default_session_timeout>
    <!-- Query log. Used only for queries with setting log_queries = 1. -->
    <query_log>

        <database>system</database>
        <table>query_log</table>
        <partition_by>toYYYYMM(event_date)</partition_by>
        <!-- Interval of flushing data. -->
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <!-- Maximal size in lines for the logs. When non-flushed logs amount reaches max_size, logs dumped to the disk. -->
        <max_size_rows>1048576</max_size_rows>
        <!-- Pre-allocated size in lines for the logs. -->
        <reserved_size_rows>8192</reserved_size_rows>
        <!-- Lines amount threshold, reaching it launches flushing logs to the disk in background. -->
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <!-- Indication whether logs should be dumped to the disk in case of a crash -->
        <flush_on_crash>false</flush_on_crash>
    </query_log>

    <trace_log>
        <database>system</database>
        <table>trace_log</table>

        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <!-- Indication whether logs should be dumped to the disk in case of a crash -->
        <flush_on_crash>false</flush_on_crash>
    </trace_log>
    <query_thread_log>
        <database>system</database>
        <table>query_thread_log</table>
        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <flush_on_crash>false</flush_on_crash>
    </query_thread_log>

    <query_views_log>
        <database>system</database>
        <table>query_views_log</table>
        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
    </query_views_log>

    <part_log>
        <database>system</database>
        <table>part_log</table>
        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <flush_on_crash>false</flush_on_crash>
    </part_log>

    <metric_log>
        <database>system</database>
        <table>metric_log</table>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <collect_interval_milliseconds>1000</collect_interval_milliseconds>
        <flush_on_crash>false</flush_on_crash>
    </metric_log>

    <asynchronous_metric_log>
        <database>system</database>
        <table>asynchronous_metric_log</table>
        <flush_interval_milliseconds>7000</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <flush_on_crash>false</flush_on_crash>
    </asynchronous_metric_log>
    <opentelemetry_span_log>
        <engine>
            engine MergeTree
            partition by toYYYYMM(finish_date)
            order by (finish_date, finish_time_us, trace_id)
        </engine>
        <database>system</database>
        <table>opentelemetry_span_log</table>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <flush_on_crash>false</flush_on_crash>
    </opentelemetry_span_log>


    <!-- Crash log. Stores stack traces for fatal errors.
         This table is normally empty. -->
    <crash_log>
        <database>system</database>
        <table>crash_log</table>

        <partition_by />
        <flush_interval_milliseconds>1000</flush_interval_milliseconds>
        <max_size_rows>1024</max_size_rows>
        <reserved_size_rows>1024</reserved_size_rows>
        <buffer_size_rows_flush_threshold>512</buffer_size_rows_flush_threshold>
        <flush_on_crash>true</flush_on_crash>
    </crash_log>

    <!-- Profiling on Processors level. -->
    <processors_profile_log>
        <database>system</database>
        <table>processors_profile_log</table>

        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <flush_on_crash>false</flush_on_crash>
    </processors_profile_log>

    <asynchronous_insert_log>
        <database>system</database>
        <table>asynchronous_insert_log</table>

        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <max_size_rows>1048576</max_size_rows>
        <reserved_size_rows>8192</reserved_size_rows>
        <buffer_size_rows_flush_threshold>524288</buffer_size_rows_flush_threshold>
        <flush_on_crash>false</flush_on_crash>
        <partition_by>event_date</partition_by>
        <ttl>event_date + INTERVAL 3 DAY</ttl>
    </asynchronous_insert_log>

    <backup_log>
        <database>system</database>
        <table>backup_log</table>
        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
    </backup_log>

    <s3queue_log>
        <database>system</database>
        <table>s3queue_log</table>
        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
    </s3queue_log>

    <blob_storage_log>
        <database>system</database>
        <table>blob_storage_log</table>
        <partition_by>toYYYYMM(event_date)</partition_by>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
        <ttl>event_date + INTERVAL 30 DAY</ttl>
    </blob_storage_log>

    <top_level_domains_lists>
    </top_level_domains_lists>

    <dictionaries_config>*_dictionary.*ml</dictionaries_config>
    <dictionaries_lazy_load>true</dictionaries_lazy_load>
    <wait_dictionaries_load_at_startup>true</wait_dictionaries_load_at_startup>
    <!-- Configuration of user defined executable functions -->
    <user_defined_executable_functions_config>*_function.*ml</user_defined_executable_functions_config>

    <encryption_codecs>
    </encryption_codecs>


    <distributed_ddl>
        <!-- Path in ZooKeeper to queue with DDL queries -->
        <path>{{ ck_datadir }}/task_queue/ddl</path>
    </distributed_ddl>
    <graphite_rollup_example>
        <pattern>
            <regexp>click_cost</regexp>
            <function>any</function>
            <retention>
                <age>0</age>
                <precision>3600</precision>
            </retention>
            <retention>
                <age>86400</age>
                <precision>60</precision>
            </retention>
        </pattern>
        <default>
            <function>max</function>
            <retention>
                <age>0</age>
                <precision>60</precision>
            </retention>
            <retention>
                <age>3600</age>
                <precision>300</precision>
            </retention>
            <retention>
                <age>86400</age>
                <precision>3600</precision>
            </retention>
        </default>
    </graphite_rollup_example>
    <format_schema_path>{{ ck_datadir }}/format_schemas/</format_schema_path>
    <google_protos_path>/usr/share/clickhouse/protos/</google_protos_path>

    <send_crash_reports>
        <enabled>false</enabled>
        <!-- Change <anonymize> to true if you don't feel comfortable attaching the server hostname to the crash report -->
        <anonymize>false</anonymize>
        <!-- Default endpoint should be changed to different Sentry DSN only if you have -->
        <!-- some in-house engineers or hired consultants who're going to debug ClickHouse issues for you -->
        <endpoint>https://6f33034cfe684dd7a3ab9875e57b1c8d@o388870.ingest.sentry.io/5226277</endpoint>
    </send_crash_reports>
    <!-- Configuration for the query cache -->
    <query_cache>
        <max_size_in_bytes>1073741824</max_size_in_bytes>
        <max_entries>1024</max_entries>
        <max_entry_size_in_bytes>1048576</max_entry_size_in_bytes>
        <max_entry_size_in_rows>30000000</max_entry_size_in_rows>
    </query_cache>
    <backups>
        <allowed_path>backups</allowed_path>
        <remove_backup_files_after_failure>true</remove_backup_files_after_failure>
    </backups>
</clickhouse>
